# Project configuration
PROJECT_NAME := file_monitor

# Source files in src directory
SRC_DIR := src
OBJ_DIR := Obj
SOURCES := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/*.cpp)

# Object files in Obj directory
OBJS := $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
OBJS := $(OBJS:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)

# Target type: EXECUTABLE or SHARED_LIBRARY
TARGET_TYPE := EXECUTABLE

# Output configuration
ifeq ($(TARGET_TYPE),EXECUTABLE)
    TARGET := $(PROJECT_NAME)
else ifeq ($(TARGET_TYPE),SHARED_LIBRARY)
    TARGET := lib$(PROJECT_NAME).so
    LDFLAGS += -shared
endif

# Project-specific flags
PROJECT_CFLAGS := -std=c11 -pthread
PROJECT_LDFLAGS := -lsqlite3 -lpthread

# Add project-specific flags to global flags
CFLAGS += $(PROJECT_CFLAGS)
LDFLAGS += $(PROJECT_LDFLAGS)

.PHONY: all clean strip

# Default target - build and strip
all: $(TARGET) strip

# Build executable or shared library
$(TARGET): $(OBJS)
	$(CC) $(OBJS) -o $@ $(LDFLAGS)

# Create Obj directory
$(OBJ_DIR):
	@mkdir -p $(OBJ_DIR)

# Compile C source files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
	$(CC) $(CFLAGS) -c $< -o $@

# Compile C++ source files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
	$(CXX) $(CXXFLAGS) -c $< -o $@

# Strip built target
strip: $(TARGET)
	@if [ -f $(TARGET) ] && command -v $(STRIP) >/dev/null 2>&1; then \
		$(STRIP) $(TARGET); \
	fi

# Clean build artifacts
clean:
	@rm -rf $(OBJ_DIR) $(TARGET)
